import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;


/**
 * 非完整，直接看2
 */
public class ThreadTest3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executors = Executors.newFixedThreadPool(3);
        ArrayList<Future<Boolean>> futures = new ArrayList<>();
        AtomicBoolean hasFlag = new AtomicBoolean(false);

        for (int i = 0; i < 3; i++) {
            futures.add(executors.submit(() -> {
                Thread.sleep(3000);
                boolean result = Math.random() > 0.5;
                if (!result) {
                    hasFlag.compareAndSet(false, true);
                }
                return result;
            }));
        }
        boolean allTrust = true;
        for (Future<Boolean> future : futures) {
            System.out.println("1111111111111111111");
            if (hasFlag.get()) {
                break;
            }
            Boolean result = future.get();
            System.out.println("222222222222222222222222");
            if (!result) {
                allTrust = false;
                break;
            }
        }

        if (hasFlag.get()) {
            executors.shutdownNow();
        } else {
            executors.shutdown();
        }
        if (allTrust) {
            System.out.println("allTrue");
        } else {
            System.out.println("false");
        }
    }
}
